/*
 * Copyright (c) 2017, Li Bo/Alex Lee All rights reserved.
 * 
 * http://www.libo4dream.club/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package com.hyts.bmsp.dao.resource;

import java.util.LinkedList;
import java.util.List;

import com.hyts.bmsp.model.resource.MenuInfoBean;
import com.hyts.bmsp.model.resource.MenuInfoDTO;
import com.hyts.bmsp.model.resource.MenuInfoQueryDTO;
import com.hyts.bmsp.tool.JFinalUtil;
import org.apache.commons.lang.StringUtils;


import com.jfinal.plugin.activerecord.Page;

/**
 * @ClassName: MenuInfoDAO
 * @Description: 菜单信息数据访问层对象
 * @author: LiBo/Alex Lee
 * @date: 2017年9月19日 下午11:00:30
 * @email: alexlibochn@china-pui.com.cn/alex.link@foxmail.com
 */
public class MenuInfoDAO {

	/**
	 * @fieldName: dao
	 * @fieldType: MenuInfoDAO
	 * @Description: 数据访问对象
	 */
	private static MenuInfoDAO dao = null;
	
	
	public static MenuInfoDAO getInstance(){
		if(dao == null){
			dao = new MenuInfoDAO();
		}
		return dao;
	}
	
	/**
	 * @Title: queryMenuListByCondition
	 * @Description: 根据查询条件进行查询出所有菜单的数据
	 * @param dto
	 * @return
	 * @return: Page<MenuInfoBean>
	 */
	public Page<MenuInfoBean> queryMenuListByCondition(MenuInfoQueryDTO dto){
		
		StringBuilder sql = null;
		List<Object> paramList = new LinkedList<Object>();
		//判断是否拼接查询条件
		sql = new StringBuilder("FROM TBL_MENU_INFO WHERE 1 = 1 ");
		
		if(StringUtils.isNotEmpty(dto.getMenuCode())){
			sql.append("AND MENU_CODE = ?");
			paramList.add(dto.getMenuCode());
		}
		if(StringUtils.isNotEmpty(dto.getMenuType())){
			sql.append(" AND MENU_TYPE = ? ");
			paramList.add(dto.getMenuType());
		}
		if(StringUtils.isNotEmpty(dto.getMenuName())){
			sql.append("AND MENU_NAME LIKE ?");
			paramList.add("%"+dto.getMenuName()+"%");
		}
		if(null != dto.getMenuSort()){
			sql.append("AND MENU_SORT = ?");
			paramList.add(dto.getMenuSort());
		}
		Page<MenuInfoBean> menuList = MenuInfoBean.menuDAO.paginate(dto.getPageNo(), dto.getPageSize(), "SELECT ID,MENU_CODE,MENU_NAME,MENU_SORT,MENU_TYPE,MENU_REMARK,MENU_PATH ", sql.toString(),paramList.toArray());
		return menuList;
	} 
	
	
	/**
	 * @Title: loadMenuInfo
	 * @Description: 加载菜单信息
	 * @param dto
	 * @return
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @return: MenuInfoBean
	 */
	public MenuInfoBean loadMenuInfo(MenuInfoDTO dto) throws IllegalArgumentException, IllegalAccessException{
		MenuInfoBean bean = MenuInfoBean.menuDAO.findById(dto.getId());
		return bean;
	}
	
	/**
	 * @Title: addMenuInfo
	 * @Description: 增添菜单信息
	 * @param dto
	 * @return
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @return: Boolean
	 */
	public Boolean addMenuInfo(MenuInfoDTO dto) throws IllegalArgumentException, IllegalAccessException{
		MenuInfoBean bean = new MenuInfoBean();
		MenuInfoBean model = JFinalUtil.daoUpdateSetHandler(bean, dto,MenuInfoBean.class);
		return model.save();
	}
	
	
	/**
	 * @Title: editMenuInfo
	 * @Description: 更新菜单信息
	 * @param dto
	 * @return
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @return: Boolean
	 */
	public Boolean editMenuInfo(MenuInfoDTO dto) throws IllegalArgumentException, IllegalAccessException{
		MenuInfoBean bean = MenuInfoBean.menuDAO.findById(dto.getId());
		return JFinalUtil.daoUpdateSetHandler(bean, dto).update();
	}
	
	
	/**
	 * @Title: deleteMenuInfo
	 * @Description: 删除菜单信息
	 * @param dto
	 * @return
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @return: Boolean
	 */
	public Boolean deleteMenuInfo(MenuInfoDTO dto) throws IllegalArgumentException, IllegalAccessException{
		return MenuInfoBean.menuDAO.deleteById(dto.getId());
		 
	}
}
